﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Microsoft.SharePoint;
using Spac.Contract;
using Spac.Properties;

namespace Spac
{
    [ServiceBehavior(Namespace="Spac",InstanceContextMode=InstanceContextMode.PerCall)]
    public class SpacService : ISpac
    {
        #region ISpac Members

        public byte[] GetAssembly(string fullName)
        {
            SPList list = GetList();
            SPListItem item = GetListItem(fullName, list);

            byte[] bytes = item.File.OpenBinary();

            return bytes;
        }

        public void AddAssembly(byte[] assembly)
        {
            SPList list = GetList();
            list.RootFolder.Files.Add(Guid.NewGuid().ToString(), assembly);
        }

        public void DeleteAssembly(string fullName)
        {
            SPList list = GetList();
            SPListItem item = GetListItem(fullName, list);
            item.Delete();
        }

        public string GetConfigUrl(string fullName)
        {
            SPList list = GetList();
            SPListItem item = GetListItem(fullName, list);

            object url = item[new Guid("AC682A9E-93DF-4db6-BD62-BF5F3DB6D819")];

            return url==null ? null : url.ToString();
        }

        public List<ApplicationInfo> GetApplications()
        {
            List<ApplicationInfo> applications = new List<ApplicationInfo>();

            SPList list = GetList();
            SPQuery query = new SPQuery();
            query.Query = @"
                <Where>
                    <Eq>
                         <FieldRef Name='AssemblyType' />
                         <Value Type='Text'>Executable</Value>
                    </Eq>
               </Where>";

            SPListItemCollection result = list.GetItems(query);
            
            foreach (SPListItem item in result)
            {
                ApplicationInfo app = new ApplicationInfo();
                app.ApplicationName = item[SPBuiltInFieldId.Title].ToString();
                app.AssemblyFullName = item[SPBuiltInFieldId.Title].ToString();
                object config = item[new Guid("AC682A9E-93DF-4db6-BD62-BF5F3DB6D819")];
                app.ConfigurationUrl = config == null ? null : config.ToString();

                applications.Add(app);
            }

            return applications;
        }

        private static SPList GetList()
        {
            SPSite site = new SPSite(Settings.Default.SPWebUrl);
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists[Settings.Default.SPListName];
            return list;
        }

        private static SPListItem GetListItem(string fullName, SPList list)
        {
            SPQuery query = new SPQuery();
            query.Query = string.Format(@"
                <Where>
                    <Eq>
                         <FieldRef Name='Title' />
                         <Value Type='Text'>{0}</Value>
                    </Eq>
               </Where>
            ", fullName);

            SPListItemCollection result = list.GetItems(query);
            if (result.Count == 0)
                return null;

            if (result.Count > 1)
                throw new FaultException("Duplicate Assemblies found!");

            SPListItem item = result[0];
            return item;
        }



        #endregion
    }
}
